/**
 * 
 */
package ro.dta.idbi.patterns;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ro.dta.idbi.interfaces.IntegrationOperation;
import ro.dta.idbi.model.AbstractPattern;
import ro.dta.idbi.model.AbstractPrimitivePattern;
import uk.ac.ic.doc.automed.IntegrityException;
import uk.ac.ic.doc.automed.NotFoundException;
import uk.ac.ic.doc.automed.reps.Schema;

/**
 * <b>Reverse transformation for the column to table transformation pattern</b><br />
 * 
 * Example of use:<br />
 * 
 * <code>table_to_col (&lt;&lt;newTable&gt;&gt;,&lt;&lt;origTable,col&gt;&gt;)</code>
 * <br />
 * 
 * @author Tudor Dobrila
 * 
 */
public class ColToTableRev extends AbstractPattern {

	/**
	 * Name of the pattern
	 */
	public static final String NAME = "Table to Column";

	/**
	 * Description of the pattern
	 */
	public static final String DESCRIPTION = "Reverse transformation of the column to table pattern";

	/**
	 * Name of the command associated with the pattern
	 */
	public static final String COMMAND = "table_to_col";

	/**
	 * Integration phase during which the command is applied
	 */
	public static final EnumSet<IntegrationOperation> OPERATION = EnumSet
			.of(IntegrationOperation.CONFORMING);

	/**
	 * Name of the table which will be removed
	 */
	private transient String newTableStr;

	/**
	 * Name of the column in the original table
	 */
	private transient String origColStr;

	/**
	 * Table to column pattern class constructor
	 * 
	 * @param sourceSchema
	 *            Source schema over which the transformation is applied
	 */
	public ColToTableRev(final Schema sourceSchema) throws NotFoundException, IntegrityException {
		super(sourceSchema);

		pattern = Pattern.compile("^" + COMMAND + "[ ]*\\((" + TABLE_REGEX + ")" + COMMA_REGEX + "("
				+ COL_REGEX + ")\\)$");
	}

	/**
	 * Table to column pattern class constructor
	 * 
	 * @param sourceSchema
	 *            Name of the source schema over which the transformation is
	 *            applied
	 */
	public ColToTableRev(final String sourceSchema) throws NotFoundException, IntegrityException {
		this(Schema.getSchema(sourceSchema));
	}
	
	@Override
	protected List<String> getPrimitiveCommands() throws NotFoundException, IntegrityException {
		ColToTable forward = new ColToTable(sourceSchema, origColStr, newTableStr);
		List<String> commands = forward.getPrimitiveCommands();
		List<String> result = new ArrayList<String>();
		for (String cmd : commands) {
			result.add(0, AbstractPrimitivePattern.getReverseTrans(cmd));
		}
		
		return result;
	}

	@Override
	protected boolean verify() {
		// TODO Auto-generated method stub
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ro.dta.idbi.model.TransformationPattern#parse(java.lang.String)
	 */
	@Override
	protected void parse(final String trans) {
		final Matcher matcher = pattern.matcher(trans);
		matcher.find();

		origColStr = matcher.group(2);
		newTableStr = parseTableName(matcher.group(1));
	}

}
